print(int('123'))
print(float('12.34'))
print(type(str(1.23)))
print(type(str(100)))

n1 = 255
n2 = 1000
print(hex(n1))
print(hex(n2))

def my_abs(x):
    if not isinstance(x, (int, float)):
        raise TypeError('bad operand type')
    if x >= 0:
        return x
    else:
        return -x

print(my_abs(-9.2))

# print(my_abs('A'))

import math

def quadratic(a, b, c):
    if b ** 2 - 4 * a * c < 0 :
       print("无根")
    return ((-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a), (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a))
# 测试:
print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))

if quadratic(2, 3, 1) != (-0.5, -1.0):
    print('测试失败')
elif quadratic(1, 3, -4) != (1.0, -4.0):
    print('测试失败')
else:
    print('测试成功')


print('=' * 10)
def add_end(L=[]):
    L.append('END')
    return L

print(add_end())
print(add_end())

# 默认参数必须指向不可变参数，多次调用才不会出错
def add_end(L=None):
    if L is None:
        L = []
    L.append('END')
    return L

print(add_end())
print(add_end())


print('=' * 10)
# 可变参数，用*表示
def calc(*numbers):
    sum = 0
    for n in numbers:
        sum += n * n
    return sum

print(calc(1, 2, 3))

nums = [1, 2, 3]
print(calc(*nums))

    
print('=' * 10)
# 关键字参数**kw，可以传入任意多个含参数名的参数
def person(name, age, **kw):
    print(f'name: {name}, age: {age}, other: {kw}')

person('lzf', 30, city = 'Beijing')

extra = {'city': 'Beijing', 'job': 'Engineer'}
person('lzf', 33, **extra)

# 命名关键字参数
# 由于关键字参数可以传入不限制的关键字，但是如果要指定参数的命名，就需要使用命名关键字参数
# 命名关键字参数需要用一个*作为分隔符，*后面的参数才会作为命名关键字参数；如果命名关键字参数是在可变参数后面的，那么就不需要有*作为分隔符了
def person(name, age, *, city, job):
    print(name, age, city, job)

print('')
# 命名关键字参数必须传入参数名，没有参数名会报错
def person(name, age, city, job):
    print(name, age, city, job)

person('lzf', age = 22, city = 'Beijing', job = 'Engineer')

